package com.neusoft.work.service.impl;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neusoft.work.dao.DietCalendarDao;
import com.neusoft.work.dao.DishDao;
import com.neusoft.work.dto.DietCalendarDTO;
import com.neusoft.work.po.DietCalendar;
import com.neusoft.work.po.Dish;
import com.neusoft.work.po.PageResponseBean;
import com.neusoft.work.po.ResponseBean;
import com.neusoft.work.service.DietCalendarService;

@Service
public class DietCalendarServiceImpl implements DietCalendarService {

    @Autowired
    private DietCalendarDao dietCalendarDao;

    @Autowired
    private DishDao dishDao;

    @Override
    public ResponseBean<Integer> addDietCalendar(DietCalendar dc) {
        int rows = dietCalendarDao.insert(dc);
        if (rows > 0) {
            return new ResponseBean<>(dc.getId());
        } else {
            return new ResponseBean<>(500, "添加失败");
        }
    }

    @Override
    public ResponseBean<Integer> deleteDietCalendar(Integer id) {
        int rows = dietCalendarDao.deleteById(id);
        if (rows > 0) {
            return new ResponseBean<>(rows);
        } else {
            return new ResponseBean<>(500, "删除失败");
        }
    }

    @Override
    public ResponseBean<Integer> updateDietCalendar(DietCalendar dc) {
        int rows = dietCalendarDao.updateById(dc);
        if (rows > 0) {
            return new ResponseBean<>(rows);
        } else {
            return new ResponseBean<>(500, "更新失败");
        }
    }

    /**
     * 按天 + 餐次 查询，并返回 DTO（包含菜品详细信息）
     */
    @Override
    public ResponseBean<List<DietCalendarDTO>> queryDietCalendar(Integer dayOfWeek, Integer mealType) {
        // 1. 查询日历记录
        List<DietCalendar> recs = dietCalendarDao.selectList(new QueryWrapper<DietCalendar>()
            .eq("dayOfWeek", dayOfWeek)
            .eq("mealType", mealType)
        );

        // 2. 批量查询所有关联的菜品 ID
        List<Integer> dishIds = recs.stream()
            .map(DietCalendar::getDishId)
            .distinct()
            .collect(Collectors.toList());

        // —— 关键：在 if/else 中给 dishMap 赋值一次，保证它是 effectively final
        Map<Integer, Dish> dishMap;
        if (dishIds.isEmpty()) {
            dishMap = Collections.emptyMap();
        } else {
            List<Dish> dishes = dishDao.selectBatchIds(dishIds);
            dishMap = dishes.stream()
                .collect(Collectors.toMap(Dish::getId, d -> d));
        }

        // 3. 映射到 DTO
        List<DietCalendarDTO> dtos = recs.stream().map(r -> {
            DietCalendarDTO dto = new DietCalendarDTO();
            BeanUtils.copyProperties(r, dto);
            Dish d = dishMap.get(r.getDishId());
            if (d != null) {
                dto.setName(d.getName());
                dto.setCategory(d.getCategory());
                dto.setPrice(d.getPrice());
                dto.setImageUrl(d.getImageUrl());
            }
            return dto;
        }).collect(Collectors.toList());

        // 4. 返回
        PageResponseBean<List<DietCalendarDTO>> rb = new PageResponseBean<>(dtos);
        rb.setTotal(dtos.size());
        return rb;
    }



}
